package puppy.graph;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

import javax.swing.JFrame;

import puppy.query.QueryNGramExtractor;
import util.io.FileInput;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;

public class CreateGraphOnlyTags  {

	private static final Integer EDGE_THRESHOLD = 0;
	private static final Integer NODE_THRESHOLD = 3;
	
	
	private static final Integer EDGE_THRESHOLD_KIDS = 2;
	private static final Integer NODE_THRESHOLD_KID = 3;
	
	
	/**
	 * @param args
	 */
	public int n_nodes = 0;
	public int n_edges = 0;

	// Generate
	// id_url, id_tag, url,tag,freq_tag,freq_url, freq_ut,type
	
	
	//http://5.ua/	tv_news	adults	11953	10	3	5.ua


	public UndirectedSparseGraph<DeliciousNodeSimple, DeliciousEdgeSimple> graph= null;
	public QueryNGramExtractor query_phraser = new QueryNGramExtractor();
	
	private int _id_a = 0;
	private int _id_b = 1;
	private int _tagA = 2;
	private int _tagB = 3;
	private int _freq_b= 5;
	private int _freq_a = 4;
	private int _freq_ab = 6;
	private int _age = 7;
			
		
	private int _adult_freq_a= 7;
	private int _adult_freq_b=8;
	private int _adult_freq_ab=9;
	
	
	
	AgeAggregator agg= null;

  public Hashtable<Integer,DeliciousNodeSimple> tag_names = new Hashtable<Integer,DeliciousNodeSimple>(); 
	
	
	public CreateGraphOnlyTags(AgeAggregator agg){
		this.agg=agg;
		
	}
	
	public CreateGraphOnlyTags(){
		
	}
	
	//kids kids-teens teens teens-mteens mteesn all adults
	
	int j =0;
	public void initGraph(String path) {

		FileInput in = new FileInput(path);
		graph = new UndirectedSparseGraph<DeliciousNodeSimple, DeliciousEdgeSimple>();
		String line = in.readString();
		int line_count=0;
		
		while (line != null) {
		//	System.out.println("Adding " + line);
			dumpLineIntoGraphDirected(graph, line);
			line = in.readString();
			
			//System.out.println(" j: "+ j + "\t line_count:" + line_count);
			line_count++;
	//	if(j==5) return;
		}
	}

	
	
	
	private void dumpLineIntoGraphDirected(
			UndirectedSparseGraph<DeliciousNodeSimple, DeliciousEdgeSimple> graph,
			String line) {

		String[] fields = line.split("\t");

		Integer freq_ut = Integer.valueOf(Integer.valueOf(fields[_freq_ab]));
		
		if(freq_ut<=EDGE_THRESHOLD){
			
			return;
		}
		
		j++;
		Integer id_a = Integer.valueOf(Integer.valueOf(fields[_id_a]));
		Integer id_b = Integer.valueOf(Integer.valueOf(fields[_id_b]));
		DeliciousNodeSimple a =null;
		
		DeliciousNodeSimple b =null;
		
		

		
		
		if(!tag_names.containsKey(id_a)){
			a= new DeliciousNodeSimple();
			String name_a = fields[_tagA];
			a.name=name_a;
			Integer freq_a = Integer.valueOf(Integer.valueOf(fields[_freq_a]));
			a.freq=freq_a;
			
			
			tag_names.put(id_a, a);
			
			if(name_a.contains("_")){
			//	query_phraser.addItem(name_a);
				}
			
			
			if(fields.length> _adult_freq_ab){
				
				int adult_freq_tagA= Integer.valueOf(Integer.valueOf(fields[_adult_freq_a]));
				a.freq_adult=adult_freq_tagA;
			//	adult_freq_domain= Integer.valueOf(Integer.valueOf(fields[_adult_freq_ab]));
				
			}
			
			
		}else{
			
			
			a= tag_names.get(id_a);
		}
		
		if(!tag_names.containsKey(id_b)){
			b= new DeliciousNodeSimple();
			String name_b = fields[_tagB];
			b.name=name_b;
			
			Integer freq_b = Integer.valueOf(Integer.valueOf(fields[_freq_b]));
			b.freq=freq_b;
			tag_names.put(id_a, b);
			
			if(name_b.contains("_")){
			//	query_phraser.addItem(name_a);
				}
			
			if(fields.length> _adult_freq_ab){
				
				int adult_freq_tagB= Integer.valueOf(Integer.valueOf(fields[_adult_freq_b]));
				b.freq_adult=adult_freq_tagB;
			//	adult_freq_domain= Integer.valueOf(Integer.valueOf(fields[_adult_freq_ab]));
				
			}
			
			
		}else{
			
			b=tag_names.get(id_b);
		}
		
			
		

		
		//adding edge info
		//add edge
		
		DeliciousEdgeSimple edge  = new DeliciousEdgeSimple();
		edge.freq=freq_ut;
		
		//add adult ut linking
		// TODO --------------------
				
		graph.addEdge(edge, a,b);

			
			//create edge between both of them
		fields=null;
	}


	
	
	
	
}
